package chenji.itemeditor.util.asmutil;

import chenji.itemeditor.nbt.base.NBTBaseType;
import chenji.itemeditor.nbt.base.NBTTagBoolean;
import chenji.itemeditor.nbt.base.NBTTagByte;
import chenji.itemeditor.nbt.base.NBTTagByteArray;
import chenji.itemeditor.nbt.base.NBTTagDouble;
import chenji.itemeditor.nbt.base.NBTTagEnd;
import chenji.itemeditor.nbt.base.NBTTagFloat;
import chenji.itemeditor.nbt.base.NBTTagInt;
import chenji.itemeditor.nbt.base.NBTTagIntArray;
import chenji.itemeditor.nbt.base.NBTTagLong;
import chenji.itemeditor.nbt.base.NBTTagShort;
import chenji.itemeditor.nbt.base.NBTTagString;
import chenji.itemeditor.util.asmtool.ASMTool;
import net.minecraft.server.v1_9_R2.NBTBase;
import net.minecraft.server.v1_9_R2.NBTTagCompound;
import net.minecraft.server.v1_9_R2.NBTTagList;

public class NBTTagTool implements ASMTool {
	private static int version = 1;
	private static String name = "v1_9_R2";

	public Object asNMSNBTBaseCopy(chenji.itemeditor.nbt.base.NBTBase b) {
		if (b == null) {
			return null;
		}
		switch (b.getType()) {
		case INTEGER:
			return new net.minecraft.server.v1_9_R2.NBTTagInt(((NBTTagInt) b).getValue());
		case BYTE:
			return new net.minecraft.server.v1_9_R2.NBTTagByte(((NBTTagByte) b).getValue());
		case BYTEARRAY:
			return new net.minecraft.server.v1_9_R2.NBTTagByteArray(((NBTTagByteArray) b).getValue());
		case DOUBLE:
			return new net.minecraft.server.v1_9_R2.NBTTagDouble(((NBTTagDouble) b).getValue());
		case FLOAT:
			return new net.minecraft.server.v1_9_R2.NBTTagFloat(((NBTTagFloat) b).getValue());
		case INTARRAY:
			return new net.minecraft.server.v1_9_R2.NBTTagIntArray(((NBTTagIntArray) b).getValue());
		case LONG:
			return new net.minecraft.server.v1_9_R2.NBTTagLong(((NBTTagLong) b).getValue());
		case NBTTAGCOMPOUND:
			return this.asNMSNBTTagCompoundCopy(b);
		case NBTTAGLIST:
			return this.asNMSNBTTagListCopy(b);
		case SHORT:
			return new net.minecraft.server.v1_9_R2.NBTTagShort(((NBTTagShort) b).getValue());
		case STRING:
			return new net.minecraft.server.v1_9_R2.NBTTagString(((NBTTagString) b).getValue());
		default:
			return null;
		}
	}

	public Object asNMSNBTTagListCopy(chenji.itemeditor.nbt.base.NBTBase b) {
		if (b == null) {
			return null;
		}
		if (!(b.getType().equals(NBTBaseType.NBTTAGLIST))) {
			return new NBTTagList();
		}
		chenji.itemeditor.nbt.base.NBTTagList list = (chenji.itemeditor.nbt.base.NBTTagList) b;
		NBTTagList nmslist = new NBTTagList();
		int i = 0;
		int size = list.size() - 1;
		while (i <= size) {
			chenji.itemeditor.nbt.base.NBTBase base = list.get(i);
			if (base == null) {
				nmslist.add(null);
				continue;
			}
			if (base.getType().equals(NBTBaseType.BOOLEAN)) {
				continue;
			}
			NBTBase finalBase = (NBTBase) this.asNMSNBTBaseCopy(base);
			nmslist.add(finalBase);
			i++;
		}
		return nmslist;
	}

	public Object asNMSNBTTagCompoundCopy(chenji.itemeditor.nbt.base.NBTBase b) {
		if (b == null) {
			return null;
		}
		if (!(b.getType().equals(NBTBaseType.NBTTAGCOMPOUND))) {
			return new NBTTagCompound();
		}
		chenji.itemeditor.nbt.base.NBTTagCompound tag = ((chenji.itemeditor.nbt.base.NBTTagCompound) b);
		NBTTagCompound nmstag = new NBTTagCompound();
		for (String s : tag.keySet()) {
			chenji.itemeditor.nbt.base.NBTBase base = tag.get(s);
			if (base == null) {
				continue;
			}
			if (base.getType().equals(NBTBaseType.BOOLEAN)) {
				NBTTagBoolean finalBase = (NBTTagBoolean) base;
				nmstag.setBoolean(s, finalBase.getValue());
				continue;
			}
			NBTBase finalBase = (NBTBase) this.asNMSNBTBaseCopy(base);
			nmstag.set(s, finalBase);
		}
		return nmstag;
	}

	public chenji.itemeditor.nbt.base.NBTTagCompound asLocalNBTTagCompoundCopy(Object obj) {
		if (obj == null) {
			return null;
		}
		if (!(obj instanceof NBTTagCompound)) {
			return new chenji.itemeditor.nbt.base.NBTTagCompound();
		}
		NBTTagCompound tag = (NBTTagCompound) obj;
		chenji.itemeditor.nbt.base.NBTTagCompound localtag = new chenji.itemeditor.nbt.base.NBTTagCompound();
		for (String s : tag.c()) {
			NBTBase b = tag.get(s);
			if (b == null) {
				continue;
			}
			chenji.itemeditor.nbt.base.NBTBase localbase = this.asLocalNBTBaseCopy(b);
			localtag.set(s, localbase);
		}
		return localtag;
	}

	public chenji.itemeditor.nbt.base.NBTTagList asLocalNBTTagListCopy(Object obj) {
		if (obj == null) {
			return null;
		}
		if (!(obj instanceof NBTTagList)) {
			return new chenji.itemeditor.nbt.base.NBTTagList();
		}
		NBTTagList list = (NBTTagList) obj;
		chenji.itemeditor.nbt.base.NBTTagList localtag = new chenji.itemeditor.nbt.base.NBTTagList();
		int index = list.size() - 1;
		if (index < 0) {
			return localtag;
		}
		int nowindex = 0;
		while (nowindex <= index) {
			localtag.add(this.asLocalNBTBaseCopy(list.h(nowindex)));
			nowindex++;
		}
		return localtag;
	}

	public chenji.itemeditor.nbt.base.NBTBase asLocalNBTBaseCopy(NBTBase base) {
		if (base == null) {
			return null;
		}
		byte b = base.getTypeId();
		switch (b) {
		case 0:
			return new NBTTagEnd();
		case 1:
			return new NBTTagByte(((net.minecraft.server.v1_9_R2.NBTTagByte) base).f());
		case 2:
			return new NBTTagShort(((net.minecraft.server.v1_9_R2.NBTTagShort) base).e());
		case 3:
			return new NBTTagInt(((net.minecraft.server.v1_9_R2.NBTTagInt) base).d());
		case 4:
			return new NBTTagLong(((net.minecraft.server.v1_9_R2.NBTTagLong) base).c());
		case 5:
			return new NBTTagFloat(((net.minecraft.server.v1_9_R2.NBTTagFloat) base).h());
		case 6:
			return new NBTTagDouble(((net.minecraft.server.v1_9_R2.NBTTagDouble) base).g());
		case 7:
			return new NBTTagByteArray(((net.minecraft.server.v1_9_R2.NBTTagByteArray) base).c());
		case 11:
			return new NBTTagIntArray(((net.minecraft.server.v1_9_R2.NBTTagIntArray) base).c());
		case 8:
			return new NBTTagString(((net.minecraft.server.v1_9_R2.NBTTagString) base).toString());
		case 9:
			return this.asLocalNBTTagListCopy((NBTTagList) base);
		case 10:
			return this.asLocalNBTTagCompoundCopy((NBTTagCompound) base);
		}
		return null;
	}

	@Override
	public String getNMSVersion() {
		return name;
	}

	@Override
	public int getVersion() {
		return version;
	}
}
